package biz.datalk.industrialland.admin.modules.externalapi.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tarofang@163.com
 * @date 2024-01-15
 */
@Getter
@Setter
@ToString
public class ExternalApiProperties {

    /** 是否有效 */
    private Boolean enable = true;

    /** 是否使用代理 */
    private Boolean useProxy = false;
    /** 代理的 schema */
    private String proxySchema;
    /** 代理 host */
    private String proxyHost;
    /** 代理 端口 */
    private Integer proxyPort = 0;
    /** 是否跳过 SSL 验证 */
    private Boolean skipSslVerify = true;
    /** 自定义请求头 */
    private Map<String, String> headers = new HashMap<>();

    protected void checkValid(String prefix) {
        if (!enable) {
            return;
        }
        if (this.useProxy != null && useProxy) {
            Assert.isTrue(StringUtils.isNotBlank(this.proxySchema), () -> "The '" + prefix + ".proxy-schema' must not be blank.");
            Assert.isTrue(StringUtils.isNotBlank(this.proxyHost), () -> "The '" + prefix + ".proxy-host' must not be blank.");
            Assert.isTrue(this.proxyPort != null && this.proxyPort > 0, () -> "The '" + prefix + ".proxy-port' must be greater than zero.");
            Assert.notNull(this.skipSslVerify, () -> "The '" + prefix + ".skip-ssl-verify' must not be null.");
        }
    }

}
